package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.echoesnet.echoesshortvideo.Beans.IncludeNavBarBean;
import com.echoesnet.echoesshortvideo.Persistence.BlackListBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.ViewModels.FansFocusViewModel;
import com.echoesnet.echoesshortvideo.databinding.ActivityBlackMangerBinding;
import com.echoesnet.echoesshortvideo.views.adapters.BlackListAdapter;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.ui.emptyview.emptyViewOnShownListener;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BlackMangerAct extends BaseActivity
{
    private static final String TAG = BlackMangerAct.class.getSimpleName();

    @BindView(R.id.rv_black)
    UltimateRecyclerView rvBlack;

    private Unbinder unbinder;
    private BlackListAdapter aDapter;
    private Activity mAct;
    private List<BlackListBean> mList = new ArrayList<>();
    private String uId;
    private FansFocusViewModel uViewModel;
    private int INDEX = 0;
    private int NUM = 20;
    private ActivityBlackMangerBinding actBinding;
    private IncludeNavBarBean navBean;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mAct = this;
        actBinding = DataBindingUtil.setContentView(this, R.layout.activity_black_manger);
        navBean = new IncludeNavBarBean(getString(R.string.blackList), true);
        actBinding.relNavBar.setNavBean(navBean);
        actBinding.relNavBar.itvLeft.setOnClickListener(view -> finish());

        unbinder = ButterKnife.bind(this);
        uId = getIntent().getStringExtra("uId");

        if (aDapter==null)
        aDapter = new BlackListAdapter(this, mList);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvBlack.setLayoutManager(linearLayoutManager);
        rvBlack.reenableLoadmore();
        rvBlack.setHasFixedSize(false);
        aDapter.setCustomLoadMoreView(LayoutInflater.from(this).inflate(R.layout.bottom_progressbar, null));
        rvBlack.setAdapter(aDapter);
        rvBlack.hideEmptyView();
        rvBlack.setEmptyView(R.layout.common_empty_view, UltimateRecyclerView.EMPTY_CLEAR_ALL, new emptyViewOnShownListener()
        {
            @Override
            public void onEmptyViewShow(View mView)
            {
                ((TextView) mView.findViewById(R.id.tvDesc)).setText("黑名单里还没有人呢~");
            }
        });

        rvBlack.setOnLoadMoreListener((itemsCount, maxLastVisiblePosition) -> {
            INDEX += NUM;
            uViewModel.requestBlackList(INDEX, NUM);
        });
        rvBlack.setDefaultOnRefreshListener(() -> {
            INDEX = 0;
            uViewModel.requestBlackList(INDEX, NUM);
        });
        aDapter.setOnItemClickListener((view, infoEntity) -> {
            switch (view.getId())
            {
                case R.id.tvNickName:
                    break;
                case R.id.tv_remove:
                    //移除按钮
                    uViewModel.removeBlack(infoEntity);
                    break;
            }
        });

        uViewModel = ViewModelProviders.of(this).get(FansFocusViewModel.class);
        uViewModel.getBlackDetail().observe(this, new Observer<List<BlackListBean>>()
        {
            @Override
            public void onChanged(@Nullable List<BlackListBean> list)
            {
                //更新UI
                rvBlack.setRefreshing(false);
                if (mList != null)
                {
                    // 添加去重复 ====
                    for (BlackListBean fansBean : list)
                    {
                        if (mList.contains(fansBean))
                        {
                            int index = mList.indexOf(fansBean);
                            mList.remove(index);
                        }
                        mList.add(fansBean);
                        aDapter.notifyDataSetChanged();
                    }
                    if (mList.isEmpty())
                    {
                        rvBlack.showEmptyView();
                    } else
                    {
                        rvBlack.hideEmptyView();
                    }

                }
            }
        });

        uViewModel.getRemoveBack().observe(this, new Observer<BlackListBean>()
        {
            @Override
            public void onChanged(@Nullable BlackListBean bean)
            {
                mList.remove(bean);
                //删除一个item
                if (mList.size()==0)
                {
                    uViewModel.requestBlackList(INDEX, NUM);
                }
                aDapter.notifyDataSetChanged();

            }
        });

        uViewModel.requestBlackList(INDEX, NUM);

    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
    }

}
